import React, { useState } from 'react';
import ReactDOM from 'react-dom';
// 自定义hook 就是编写一个以use开头的函数即可  函数名得遵循驼峰
function useAsyncAdd(init, timer = 1000) {
  let [count, setCount] = useState(init)
  function add(t) {
    setTimeout(() => {
      setCount((n) => {
        // n永远是最新的count
        return n + t
      })
    }, timer);
  }
  return [count, add]
}
function App() {
  let [count, add] = useAsyncAdd(100)
  return <div className=''>
    <h1>{count}</h1>
    <button onClick={() => {
      add(5)
    }}>+</button>
  </div>;
}


ReactDOM.render(<App />, document.getElementById('root'))